{
*******************************************************************************
-------------------------------------------------------------------------------
Kliper - next generation multimedia player
Copyright (C) Marcin 'vi4m' Kliks (vi4m@wp.pl)
Copyright (C) Tomasz 'dymsza' Odrobny (dymsza@gmail.com)
-------------------------------------------------------------------------------
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*******************************************************************************
}

unit upaski;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, Gradient;

type
  Tonchange = procedure(pos: integer) of object;

type
  tpasek = class
  private
    creating: boolean;
    gradient: TGradient;
    image: TImage;
    onchange: Tonchange;
    x_pos: integer;
    isdown: Boolean;
    min: integer;
    max: integer;
    precentpos: Integer;
    lastpos: Integer;
    enabled_var: boolean;
    function getpos: integer;
    procedure setpos(Value: integer);
    procedure on_change(x_: integer; start: Boolean);
    procedure on_mouse_down(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

    procedure setenabled(valute: boolean);

  public
    property enabled: boolean read enabled_var write setenabled;
    property pos: Integer read getpos write setpos;

    constructor create(gradient_: TGradient; image_: TImage; onchange_: Tonchange; min_, max_, defalut_pos_: Integer; color: TColor);
  end;

implementation

uses Math;

procedure tpasek.setenabled(valute: boolean);
begin
  gradient.Visible := valute;
  enabled_var := valute;
end;

procedure tpasek.setpos(Value: integer);
begin
  precentpos := round((value - min) / (max - min) * 100);
  on_change(round(((value - min) / (max - min) * image.Width)), true)
end;


function tpasek.getpos: integer;
begin
  Result := round(min + ((max - min) * precentpos) / 100);
end;

procedure tpasek.on_change(x_: integer; start: Boolean);
var
  rec_: TRect;
  temp: Integer;
begin
  precentpos := round(100 * ((x_)) / image.Width);
  gradient.Width := X_ - 2;
  temp := getpos;

  if temp <> lastpos then
    if not creating then
    begin
      onchange(temp);
    end;
  lastpos := temp;

end;

procedure tpasek.MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin

  isdown := False;


end;

procedure tpasek.MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  if enabled_var then begin

    if (isdown) then
    begin
      if x < 0 then
        on_change(0, true)
      else
      begin
        if (x < image.Width) then
          on_change(x, true);

        if (x > image.Width) then
          on_change(image.Width - 2, true);
      end;


    end;

  end;

end;


procedure tpasek.on_mouse_down(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if enabled_var then
  begin
    on_change(x, true);
    isdown := True;
  end;

end;


constructor tpasek.create(gradient_: TGradient; image_: TImage; onchange_: Tonchange; min_, max_, defalut_pos_: Integer; color: TColor);
begin

  creating := true;
  gradient := gradient_;
  image := image_;
  onchange := onchange_;
  image.Canvas.Brush.Color := clWindow;
  image.Canvas.Rectangle(0, 0, image.Width - 1, image.Height - 1);

  image.OnMouseDown := on_mouse_down;
  image.OnMouseUp := MouseUp;
  image.OnMouseMove := MouseMove;

  gradient.OnMouseDown := on_mouse_down;
  gradient.OnMouseUp := MouseUp;
  gradient.OnMouseMove := MouseMove;

  gradient.Left := image.Left + 2;
  gradient.Top := image.top + 1;
  gradient.Height := image.Height - 3;
  min := min_;
  max := max_;

  setpos(defalut_pos_);
  lastpos := defalut_pos_;
  creating := false;
  enabled_var := True;
end;

end.
